<%@page import="java.util.HashMap"%>
<%@page import="java.util.Map"%>
<%@page import="org.json.simple.JSONArray"%>
<%@page import="vo.CommunityVo"%>
<%@page import="java.util.List"%>
<%@page import="dao.CommunityDao"%>
<%@page language="java" contentType="text/plain; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
	request.setCharacterEncoding("utf-8");

	System.out.println("get_reply");

	int b_no = Integer.parseInt(request.getParameter("b_no"));
	
	String b_ref = request.getParameter("b_ref");
	String b_content=request.getParameter("b_content");
	String menu = request.getParameter("menu_no");
	String cate = request.getParameter("cate_no");
	String title = request.getParameter("title_no");
	String state=request.getParameter("reply_state");

	System.out.println("state------:"+state);
	
	System.out.println("출력1");
	System.out.println(b_no);
	System.out.println(b_ref);
	System.out.println(menu);
	System.out.println(cate);
	System.out.println(title);
	System.out.println(b_content);
	System.out.println("끝");
	
	
	CommunityDao cdao = CommunityDao.getDao();
	
	
	
	if(state!=null){
		//리플쓰기
		if(state.equals("write")){
			System.out.println("into write");
			CommunityVo cvo = new CommunityVo();
			int menu_no=Integer.parseInt(menu);
			int cate_no=Integer.parseInt(cate);
			int title_no=Integer.parseInt(title);
			int count = CommunityDao.getDao().selectReplyCount(b_no);
			
			String ip = request.getRemoteAddr();
			String alias = (String)request.getSession().getAttribute("alias");
			System.out.println("alias:"+alias);
			cvo.setB_ip(ip);
			cvo.setB_writer(alias);
			cvo.setB_ref(b_no);
			cvo.setB_no(b_no);
			cvo.setB_step(count+1);
			cvo.setB_content(b_content);
			cvo.setMenu_no(menu_no);
			cvo.setCate_no(cate_no);
			cvo.setTitle_no(title_no);
			
			System.out.println("출력2");
			System.out.println(cvo.getB_writer());
			System.out.println(cvo.getB_ref());
			System.out.println(cvo.getB_no());
			System.out.println(cvo.getB_step());
			System.out.println(cvo.getB_content());
			System.out.println(cvo.getMenu_no());
			System.out.println(cvo.getCate_no());
			System.out.println(cvo.getTitle_no());
			System.out.println("출력2끝");
			
			//데이터저장
			cdao.replyWrite(cvo);
		
			//Map으로 리플수와 no저장
			Map<String, Integer> map = new HashMap<String,Integer>();
			map.put("b_reply_count",cvo.getB_step());
			map.put("b_no", cvo.getB_no());
			
			//updateReplycount 리플수와 no
			cdao.updateReplyCount(map);
			
		}
		
		//리플수정
		else if(state.equals("modify")){
			System.out.println("into modify");
			CommunityVo cvo = new CommunityVo();
			cvo.setB_content(b_content);
			cvo.setB_no(b_no);
			cdao.modifyReplyContent(cvo);
			
			b_no=Integer.parseInt(b_ref);
		}
		
		
		//리플삭제
		else if(state.equals("delete")){
			System.out.println("into delete");
			cdao.deleteReply(b_no);
			b_no=Integer.parseInt(b_ref);
			int count=cdao.selectReplyCount(b_no);
			
			System.out.println("삭제전:"+b_no);
			
			Map<String, Integer> map = new HashMap<String, Integer>();
			map.put("b_reply_count",count);
			map.put("b_no", b_no);
			cdao.updateReplyCount(map);
		}
	}
	 
	//b_no로 reply_list받기 
	List<CommunityVo> reply_list = cdao.selectReplyList(b_no);
	
	//JsonArray에 list저장
	JSONArray jarr= new JSONArray();
	jarr.addAll(reply_list);
	
	
	out.println(jarr.toJSONString(reply_list));

%>